//
//  ViewController.m
//  ALi2
//
//  Created by 苗钧豪 on 16/8/15.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "List.h"
#import "OneTableViewCell.h"
#import "TwoTableViewCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong)NSMutableArray *array;
@property (nonatomic , strong)List *list;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@end

@implementation ViewController
//- (UITableView *)myTableView {
//    if (_myTableView == nil) {
//        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:(UITableViewStylePlain)];
//        [self.view addSubview:_myTableView];
//    }
//    return _myTableView;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self get];
    // Do any additional setup after loading the view, typically from a nib.
    [self.view addSubview:_myTableView];
        [_myTableView registerClass:[TwoTableViewCell class] forCellReuseIdentifier:@"2"];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row == 0) {
//        OneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"1"];
//        if(cell == nil){cell = [[OneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"1"];
//        }
//        return cell;
//    } else {
        TwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"2"  forIndexPath:indexPath];
//        _list = _array[indexPath.row];
        List *list = _array[indexPath.row];
//        if(cell == nil){cell = [[TwoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"2"];
//        }
        cell.musicName.text = list.name;
        cell.musicTitle.text = list.alias;
        cell.musicBox.text = list.albumName;
        cell.singerName.text = list.singerName;
        [_myTableView reloadData];
        return cell;
//    } 
}
- (void)get {
//    self.array = [NSMutableArray array];
//    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
//    session.responseSerializer = [AFHTTPResponseSerializer serializer];
//    [session GET:@"http://api.dongting.com/song/album/2401347" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
//        
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSData *data = responseObject;
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        NSArray *arr = dic[@"songList"];
//        for (NSDictionary *Dic in arr) {
//            _list = [[List alloc]initWithDic:Dic];
//            [_array addObject:_list];
//        }
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        
//    }];
    self.array = [NSMutableArray array];
//    NSURL *url = [NSURL URLWithString:@"http://api.dongting.com/song/album/2397722"];
//    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/song/album/2401347"];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:0];
            NSArray *arr = dic[@"data"][@"songList"];
            for (NSDictionary *Dic in arr) {
                _list = [[List alloc]initWithDic:Dic];
                [_array addObject:_list];
            }
        });
    }];
    [task resume];
//    [request setHTTPMethod:@"GET"];
//    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//        NSArray *arr = dic[@"songList"];
//        for (NSDictionary *Dic in arr) {
//            _list = [[List alloc]initWithDic:Dic];
//            [_array addObject:_list];
//        }
//}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
